package second.binarytree.solution05;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution05_429 {
    public List<List<Integer>> levelOrder(Node1 root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        LinkedList<Node1> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> tmp = new ArrayList<>();
            while (size-- > 0) {
                Node1 node = queue.removeFirst();
                tmp.add(node.val);
                for (Node1 n : node.children) {
                    if (n != null) queue.add(n);
                }
            }
            res.add(tmp);
        }
        return res;
    }
}

class Node1 {
    public int val;
    public List<Node1> children;

    public Node1() {
    }

    public Node1(int _val) {
        val = _val;
    }

    public Node1(int _val, List<Node1> _children) {
        val = _val;
        children = _children;
    }
}
